package com.example.clsj.netty;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @Author: cy
 * @Date: 2024/8/23
 */
public class ServerHandler extends SimpleChannelInboundHandler<String> {

    private static final Log log = LogFactory.get();

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        log.info("Received from client: {}", msg);
        // 回应客户端
        ctx.writeAndFlush("Hello, Client! Your message is: " + msg);

        ctx.close();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

        log.error("netty server exception occurred, cause : " + cause.getMessage(), cause);

        ctx.close();
    }
}
